//DaoLA
Helper.using('py.Figures', function (ns) {

    ns.Ellipse = function (params) {
        ns.Conic.call(this, params);
        ns.setType('Ellipse');

        var visual = new Kinetic.Ellipse({
            strokeWidth:2,
            stroke:'black',
            draggable:false
        });

        this.getCenter = function () {
            return new ns.MidPoint({ends:[this.getFoci()[0], this.getFoci()[1]]});
        };

        this.getMajorRadius = Helper.abstractMethod();
        this.getMinorRadius = Helper.abstractMethod();
        this.getFoci = Helper.abstractMethod();

        this.getFocusLength = function () {
            return ns.Point.distance(this.getFoci()[0], this.getFoci()[1]) / 2;
        };

        this.getEccentricity = function () {
            return this.getFocusLength() / this.getMajorRadius;
        };

        this.update = function (viewPort) {
            var foci = this.getFoci(),
                f1 = viewPort.getPixelPoint(foci[0].getPosition()),
                f2 = viewPort.getPixelPoint(foci[1].getPosition()),
                r1 = viewPort.getPixelLength(this.getMajorRadius()),
                r2 = viewPort.getPixelLength(this.getMinorRadius()),
                r = {x:r1, y:r2};

            visual.setX((f1.x+f2.x)/2);
            visual.setY((f1.y+f2.y)/2);
            visual.setRadius(r);

            var rotatingAngle = (new ns.Vector2Point({
                    head:this.getFoci()[1], tail:this.getFoci()[0]
                })).getAngleFromOx().getDegree();
            visual.setRotationDeg(-rotatingAngle);
        };

        this.visual(visual);

        this.toString = function () {
            return 'Ellipse: ';
        };
    };

    ns.Ellipse3Point = function (params) {
        ns.Ellipse.call(this, params);
        this.setType('Ellipse3Point');

        var empty = new ns.PointXY;

        params = Helper.extend({
            foci:[],
            point:empty
        }, params);

        this.formed = function () {
            return params.foci.length == 3 && params.point;
        };

        this.getFoci = function () {
            return params.foci;
        };

        this.getMajorRadius = function () {
            var af1 = ns.Point.distance(params.foci[0], params.point);
            var af2 = ns.Point.distance(params.foci[1], params.point);
            return (af1 + af2) / 2;
        };

        this.getMinorRadius = function () {
            var a = this.getMajorRadius();
            var c = this.getFocusLength();
            return Math.sqrt(a * a - c * c);
        }

        this.setFocus = function(index, point) {
            params.foci[index] = point;
        };

        this.setPoint = function(point) {
            params.point = point;
        };
    }
});
